package com.aiyan.edu.govern.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 **/
@Slf4j
@Configuration
public class RabbitmqConfig{

    /**
     * 队列Bean的名称
     */
    public static final String QUEUE_GATEWAY_ROUTES = "queue_gateway_routes";

    /**
     * 交换机Bean的名称
     */
    public static final String EXCHANGE_GATEWAY_ROUTES_FANOUT = "exchange_gateway_routes_fanout";

    /**
     * 队列名称
     */
    @Value("${aiyanedu.mq.queue}")
    public String queue_name;

    /**
     * 创建交换机，fanout类型
     *
     * @return
     */
    @Bean(EXCHANGE_GATEWAY_ROUTES_FANOUT)
    public Exchange exchange() {
        return ExchangeBuilder.fanoutExchange(EXCHANGE_GATEWAY_ROUTES_FANOUT).durable(true).build();
    }

    /**
     * 声明队列
     *
     * @return
     */
    @Bean(QUEUE_GATEWAY_ROUTES)
    public Queue queue() {
        return new Queue(queue_name);
    }

    /**
     * 绑定队列到交换机
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding binding(@Qualifier(QUEUE_GATEWAY_ROUTES) Queue queue, @Qualifier(EXCHANGE_GATEWAY_ROUTES_FANOUT) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
